package org.exeed.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.exeed.constant.PublishStatusContents;
import org.exeed.dao.ContactMapper;
import org.exeed.entity.ContactEntity;
import org.exeed.entity.ImageEntity;
import org.exeed.entity.PageSketchEntity;
import org.exeed.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class ContactService {


    @Autowired
    private ContactMapper contactMapper;


    public void save(ContactEntity entity) {
        entity.setId(JWTUtils.createId());

        if (StringUtils.isNotBlank(entity.getCountry().replaceAll("\\s*|\r|\n|\t", ""))
                && StringUtils.isNotBlank(entity.getName().replaceAll("\\s*|\r|\n|\t", ""))
                && StringUtils.isNotBlank(entity.getEmail().replaceAll("\\s*|\r|\n|\t", ""))
                && StringUtils.isNotBlank(entity.getPhone().replaceAll("\\s*|\r|\n|\t", ""))
                && StringUtils.isNotBlank(entity.getMessage().replaceAll("\\s*|\r|\n|\t", ""))) {

            entity.setUpdateTime(DateUtil.current());

            contactMapper.insert(entity);
        }
    }

    public Map selectList(Integer page, Integer size) {
        Page<ContactEntity> iPage = new Page<>(page, size);
        QueryWrapper<ContactEntity> queryWrapper = new QueryWrapper<>();
//        if (key != null && !key.equals("")) {
//            queryWrapper.like("name", key);
//            queryWrapper.orderByDesc("publish_time");
//        }
        queryWrapper.orderByDesc("update_time");
        IPage<ContactEntity> list = contactMapper.selectPage(iPage, queryWrapper);
        Map<String, Object> R = new HashMap<>();
        R.put("page", page);
        R.put("size", size);
        R.put("total", list.getTotal());
        R.put("list", list.getRecords());
        return R;
    }

    public ContactEntity selectById(Long id) {
        return contactMapper.selectById(id);
    }


    public void delete(Long id) {
        ContactEntity entity = contactMapper.selectById(id);

        if (entity != null) {
            contactMapper.deleteById(entity.getId());
        }
    }

}
